#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
对比模拟DataAndTrue实Data的Attack效果
"""

import json
import argparse
from pathlib import Path
from typing import Dict, Any


def load_report(report_path: Path) -> Dict[str, Any]:
    """LoadJSON报告"""
    with open(report_path, 'r') as f:
        return json.load(f)


def extract_metrics(report: Dict[str, Any]) -> Dict[str, Any]:
    """Extract关键指标"""
    attacks = report.get('attacks', [])
    if not attacks:
        return {}
    
    attack = attacks[0]
    fuzzy_match = attack.get('fuzzy_match', {})
    
    return {
        'exact_match_count': attack.get('exact_match', {}).get('correct_bytes', 0),
        'total_bytes': attack.get('exact_match', {}).get('total_bytes', 16),
        'exact_match_rate': attack.get('exact_match', {}).get('correct_bytes', 0) / 
                           attack.get('exact_match', {}).get('total_bytes', 16),
        'fuzzy_match_count': fuzzy_match.get('in_top_n_count', 0),
        'fuzzy_match_rate': fuzzy_match.get('success_rate', 0),
        'avg_rank': fuzzy_match.get('avg_rank', 0),
        'avg_confidence': attack.get('avg_confidence', 0),
    }


def compare_reports(synthetic_report: Path, real_report: Path):
    """对比两个报告"""
    print("=" * 70)
    print("  模拟Data vs True实Data 对比Analyze")
    print("=" * 70)
    
    # Load报告
    print("\nLoad报告...")
    synthetic = load_report(synthetic_report)
    real = load_report(real_report)
    
    # Extract指标
    synthetic_metrics = extract_metrics(synthetic)
    real_metrics = extract_metrics(real)
    
    # Display对比
    print("\n" + "=" * 70)
    print("  关键指标对比")
    print("=" * 70)
    
    print(f"\n{'指标':<30} {'模拟Data':<20} {'True实Data':<20} {'改善':<15}")
    print("-" * 70)
    
    # Exact Match
    print(f"{'精确Match数量':<30} "
          f"{synthetic_metrics['exact_match_count']}/{synthetic_metrics['total_bytes']:<20} "
          f"{real_metrics['exact_match_count']}/{real_metrics['total_bytes']:<20} "
          f"{real_metrics['exact_match_count'] - synthetic_metrics['exact_match_count']:+d} bytes")
    
    print(f"{'精确Match率':<30} "
          f"{synthetic_metrics['exact_match_rate']:.1%:<20} "
          f"{real_metrics['exact_match_rate']:.1%:<20} "
          f"{(real_metrics['exact_match_rate'] - synthetic_metrics['exact_match_rate'])*100:+.1f}%")
    
    # Fuzzy Match
    print(f"{'模糊Match数量':<30} "
          f"{synthetic_metrics['fuzzy_match_count']}/{synthetic_metrics['total_bytes']:<20} "
          f"{real_metrics['fuzzy_match_count']}/{real_metrics['total_bytes']:<20} "
          f"{real_metrics['fuzzy_match_count'] - synthetic_metrics['fuzzy_match_count']:+d} bytes")
    
    print(f"{'模糊Match率':<30} "
          f"{synthetic_metrics['fuzzy_match_rate']:.1%:<20} "
          f"{real_metrics['fuzzy_match_rate']:.1%:<20} "
          f"{(real_metrics['fuzzy_match_rate'] - synthetic_metrics['fuzzy_match_rate'])*100:+.1f}%")
    
    # Confidence
    print(f"{'平均置信度':<30} "
          f"{synthetic_metrics['avg_confidence']:.4f:<20} "
          f"{real_metrics['avg_confidence']:.4f:<20} "
          f"{real_metrics['avg_confidence'] - synthetic_metrics['avg_confidence']:+.4f}")
    
    # Rank
    if synthetic_metrics['avg_rank'] > 0 and real_metrics['avg_rank'] > 0:
        print(f"{'平均排名':<30} "
              f"{synthetic_metrics['avg_rank']:.1f:<20} "
              f"{real_metrics['avg_rank']:.1f:<20} "
              f"{real_metrics['avg_rank'] - synthetic_metrics['avg_rank']:+.1f}")
    
    print("\n" + "=" * 70)
    print("  结论")
    print("=" * 70)
    
    # 判断改善
    exact_improvement = real_metrics['exact_match_rate'] - synthetic_metrics['exact_match_rate']
    fuzzy_improvement = real_metrics['fuzzy_match_rate'] - synthetic_metrics['fuzzy_match_rate']
    conf_improvement = real_metrics['avg_confidence'] - synthetic_metrics['avg_confidence']
    
    print()
    if exact_improvement > 0.3:
        print("[OK] 精确Match率显著提升 - True实Data质量远优于模拟Data")
    elif exact_improvement > 0.1:
        print("[OK] 精确Match率Has所提升 - True实Data质量较好")
    elif exact_improvement > 0:
        print("[INFO] 精确Match率略Has提升 - True实Data质量一般")
    else:
        print("[FAIL] 精确Match率未提升 - 可能存In问题")
    
    if fuzzy_improvement > 0.2:
        print("[OK] 模糊Match率显著提升 - Attack更Has效")
    elif fuzzy_improvement > 0:
        print("[OK] 模糊Match率Has所提升")
    else:
        print("[INFO] 模糊Match率变化Not大")
    
    if conf_improvement > 0.1:
        print("[OK] 置信度大幅提升 - Trace质量明显改善")
    elif conf_improvement > 0:
        print("[OK] 置信度Has所提升")
    else:
        print("[FAIL] 置信度未提升 - RequireCheckDataOr代码")
    
    print()
    if exact_improvement > 0.3 or conf_improvement > 0.1:
        print("[SUCCESS] 结论: Attack系统InTrue实Data上表现良好，代码VerifySuccess！")
    elif exact_improvement > 0 or fuzzy_improvement > 0:
        print("[OK] 结论: Attack系统Basic可用，但还Has改进Empty间")
    else:
        print("[WARNING] 结论: True实Data未Display改善，建议Check:")
        print("   1. DataYesNo正确Load")
        print("   2. AlgorithmAndKeySettingsYesNoMatch")
        print("   3. Trace质量YesNo足够好")
    
    print()


def main():
    parser = argparse.ArgumentParser(description="对比模拟DataAndTrue实Data的Attack效果")
    parser.add_argument('synthetic_report', type=str, help='模拟Data的报告JSONFile')
    parser.add_argument('real_report', type=str, help='True实Data的报告JSONFile')
    
    args = parser.parse_args()
    
    synthetic_path = Path(args.synthetic_report)
    real_path = Path(args.real_report)
    
    if not synthetic_path.exists():
        print(f"Error: 找NotTo模拟Data报告: {synthetic_path}")
        return
    
    if not real_path.exists():
        print(f"Error: 找NotToTrue实Data报告: {real_path}")
        return
    
    compare_reports(synthetic_path, real_path)


if __name__ == "__main__":
    main()
